{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reshape the Matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: matrixReshape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #重塑矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>在 MATLAB 中，有一个非常有用的函数 <code>reshape</code> ，它可以将一个&nbsp;<code>m x n</code> 矩阵重塑为另一个大小不同（<code>r x c</code>）的新矩阵，但保留其原始数据。</p>\n",
    "\n",
    "<p>给你一个由二维数组 <code>mat</code> 表示的&nbsp;<code>m x n</code> 矩阵，以及两个正整数 <code>r</code> 和 <code>c</code> ，分别表示想要的重构的矩阵的行数和列数。</p>\n",
    "\n",
    "<p>重构后的矩阵需要将原始矩阵的所有元素以相同的<strong> 行遍历顺序 </strong>填充。</p>\n",
    "\n",
    "<p>如果具有给定参数的 <code>reshape</code> 操作是可行且合理的，则输出新的重塑矩阵；否则，输出原始矩阵。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/04/24/reshape1-grid.jpg\" style=\"width: 613px; height: 173px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[1,2],[3,4]], r = 1, c = 4\n",
    "<strong>输出：</strong>[[1,2,3,4]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/04/24/reshape2-grid.jpg\" style=\"width: 453px; height: 173px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[1,2],[3,4]], r = 2, c = 4\n",
    "<strong>输出：</strong>[[1,2],[3,4]]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == mat.length</code></li>\n",
    "\t<li><code>n == mat[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 100</code></li>\n",
    "\t<li><code>-1000 &lt;= mat[i][j] &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= r, c &lt;= 300</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reshape-the-matrix](https://leetcode.cn/problems/reshape-the-matrix/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reshape-the-matrix](https://leetcode.cn/problems/reshape-the-matrix/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2],[3,4]]\\n1\\n4', '[[1,2],[3,4]]\\n2\\n4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        listm=[]\n",
    "        listc=[]\n",
    "        a=len(mat)\n",
    "        b=len(mat[0])\n",
    "        t=c\n",
    "        u=0\n",
    "        if a*b==r*c:\n",
    "            num=a*b\n",
    "            for i in range(0,a):\n",
    "                for j in range(0,b):\n",
    "                    listm.append(mat[i][j])\n",
    "            while t<=len(listm):\n",
    "                listc.append(listm[u:t])\n",
    "                u+=c\n",
    "                t+=c\n",
    "            return(listc)\n",
    "        else:\n",
    "            return(mat)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # r,c 分别代表矩阵变换之后的行和列\n",
    "    # def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "    #     if len(mat) * len(mat[0]) != r * c:\n",
    "    #         return mat\n",
    "\n",
    "    #     temp, res = [], []\n",
    "    #     for n in mat:\n",
    "    #         temp += n\n",
    "    #     for i in range(0, len(temp), c):\n",
    "    #         res.append(temp[i:i + c])\n",
    "\n",
    "    #     return res\n",
    "\n",
    "\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        # res = [[0] * c] * r\n",
    "        res = [[0] * c for _ in range(r)]\n",
    "        if len(mat) == 0 or len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        i = 0\n",
    "        for row in mat:\n",
    "            for n in row:\n",
    "                print(row)\n",
    "                res[i // c][i % c] = n\n",
    "                i += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def matrixReshape(self, nums, r, c):\n",
    "        \"\"\"\n",
    "        :type nums: List[List[int]]\n",
    "        :type r: int\n",
    "        :type c: int\n",
    "        :rtype: List[List[int]]\n",
    "        \"\"\"\n",
    "        M, N = len(nums), len(nums[0])\n",
    "        if M * N != r * c:\n",
    "            return nums\n",
    "        res = [[0] * c for _ in range(r)]\n",
    "        row, col = 0, 0\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if col == c:\n",
    "                    row += 1\n",
    "                    col = 0\n",
    "                res[row][col] = nums[i][j]\n",
    "                col += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if r * c != m * n:\n",
    "            return mat\n",
    "        \n",
    "        ans = [[0] * c for _ in range(r)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[(i * n + j) // c][(i * n + j) % c] = mat[i][j]\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        #  先转换成一维序列\n",
    "        ls=[]\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                ls.append(mat[i][j])\n",
    "        res=[[j for j in range(c)] for i in range(r)]\n",
    "        if r*c==len(ls):  # 判断转换矩阵是否合理\n",
    "            #  合理则按顺序遍历填充\n",
    "            cur=0\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    res[i][j]=ls[cur]\n",
    "                    cur+=1\n",
    "            return res\n",
    "        else:\n",
    "            return mat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if r*c!=len(mat)*len(mat[0]):\n",
    "            return mat\n",
    "        else:\n",
    "            lst=[]\n",
    "            for i in range(len(mat)):\n",
    "                for j in range(len(mat[0])):\n",
    "                    lst.append(mat[i][j])\n",
    "            mat1=[[0 for i in range(c)] for j in range(r)]\n",
    "            p=0\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    mat1[i][j]=lst[p]\n",
    "                    p+=1\n",
    "            return mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        row, col = len(mat), len(mat[0])\n",
    "        if row*col != r*c:\n",
    "            return mat\n",
    "\n",
    "        ans = list()\n",
    "        new_row = list()\n",
    "        count = 0\n",
    "        for row in mat:\n",
    "            for item in row:\n",
    "                count += 1\n",
    "                new_row.append(item)\n",
    "                if count == c:\n",
    "                    ans.append(new_row)\n",
    "                    new_row = list()\n",
    "                    count = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        res = [[0] * c for i in range(r)]\n",
    "        for x in range(r * c):\n",
    "            res[x // c][x % c] = mat[x // n][x % n]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        # xx\n",
    "        if len(mat) == 0 or len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        #\n",
    "        ret = []\n",
    "        m, n = 0, 0\n",
    "        for i in range(r):\n",
    "            newRow = []\n",
    "            for j in range(c):\n",
    "                newRow.append(mat[m][n])\n",
    "                n += 1\n",
    "                if n == len(mat[0]):\n",
    "                    m += 1\n",
    "                    n = 0\n",
    "            ret.append(newRow)\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n =len(mat), len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        res = [[0 for _ in range(c)] for k in range(r)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                cur = mat[i][j]\n",
    "                total = i*n+j\n",
    "                x, y = divmod(total, c)\n",
    "                res[x][y]=cur\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if r * c != len(mat) * len(mat[0]):\n",
    "            return mat\n",
    "        ans = [[-1] * c for _ in range(r)]\n",
    "        for i in range(len(mat) * len(mat[0])):\n",
    "            ans[i // c][i % c] = mat[i // len(mat[0])][i % len(mat[0])]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m,n = len(mat),len(mat[0])\n",
    "        # 边界条件判断\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        ans = [[0]*c for _ in range(r)]\n",
    "        for x in range(m*n):\n",
    "            ans[x//c][x%c] = mat[x//n][x%n]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m,n=len(mat),len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        res=[i for j in mat for i in j]\n",
    "        return [res[i:i+c] for i in range(0,len(res),c)]            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        M, N = len(mat), len(mat[0])\n",
    "        if M * N != r * c:\n",
    "            return mat\n",
    "        res = [[0] * c for _ in range(r)]\n",
    "        row, col = 0, 0\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if col == c:\n",
    "                    row += 1\n",
    "                    col = 0\n",
    "                res[row][col] = mat[i][j]\n",
    "                col += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        res, stack = [], []\n",
    "        for row in mat:\n",
    "            for e in row:\n",
    "                stack.append(e)\n",
    "                if len(stack) == c:\n",
    "                    res.append(stack)\n",
    "                    stack = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        res = [[0] * c for _ in range(r)]\n",
    "        i, j = 0, 0\n",
    "        for x in mat:\n",
    "            for y in x:\n",
    "                if j == c:\n",
    "                    j = 0\n",
    "                    i += 1\n",
    "                res[i][j] = y\n",
    "                j += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if len(mat)*len(mat[0])!=r*c:\n",
    "            return mat\n",
    "        else:\n",
    "            result=[]\n",
    "            for i in range(r):\n",
    "                result.append([])\n",
    "            tem=[]\n",
    "            for list0 in mat:\n",
    "                tem=tem+list0\n",
    "            j=0\n",
    "            for i in tem:\n",
    "                if len(result[j])<c:\n",
    "                    result[j].append(i)\n",
    "                else:\n",
    "                    j+=1\n",
    "                    result[j].append(i)\n",
    "            return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        \n",
    "        matt = [[0]*c for rr in range(r)]\n",
    "        \n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                ind = i * c + j + 1\n",
    "                column = ind % n - 1 if ind % n > 0 else n - 1\n",
    "                row = (ind - column - 1)// n\n",
    "                matt[i][j] = mat[row][column]\n",
    "        return matt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        ans = [[]]\n",
    "        for x in chain.from_iterable(mat):\n",
    "            if len(ans[-1]) == c:\n",
    "                ans.append([])\n",
    "            ans[-1].append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        if r * c != m * n:\n",
    "            return mat\n",
    "\n",
    "        res = [[0] * c for _ in range(r)]\n",
    "        for i in range(m * n):\n",
    "            self.set(res, i, self.get(mat, i))\n",
    "        return res \n",
    "    \n",
    "    def get(self, matrix, index):\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "\n",
    "        i, j = index // n, index % n\n",
    "        return matrix[i][j]\n",
    "    \n",
    "    def set(self, res, index, val):\n",
    "        m, n = len(res), len(res[0])\n",
    "        i, j = index // n, index % n\n",
    "        res[i][j] = val\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        ans = [[0] * c for _ in range(r)]\n",
    "        idx = 0\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                x = idx // n\n",
    "                y = idx % n\n",
    "                ans[i][j] = mat[x][y]\n",
    "                idx += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        if m == r and n == c:\n",
    "            return mat\n",
    "        # res = []\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         res.append(mat[i][j])\n",
    "        # idx = 0\n",
    "        grid = [[0]*c for _ in range(r)]\n",
    "        # for i in range(r):\n",
    "        #     for j in range(c):\n",
    "        #         grid[i][j] = res[idx]\n",
    "        #         idx += 1\n",
    "        # return grid\n",
    "        for i in range(m*n):\n",
    "            grid[i//c][i%c] = mat[i//n][i%n]\n",
    "        return grid\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        h = len(mat)\n",
    "        w = len(mat[0])\n",
    "        if h*w != r*c:\n",
    "            return mat\n",
    "        k = 0\n",
    "        temp = []\n",
    "        reshape_mat = []\n",
    "        for i in range(h):\n",
    "            for j in range(w):\n",
    "                if k < c:\n",
    "                    temp.append(mat[i][j])\n",
    "                else:\n",
    "                    reshape_mat.append(temp)\n",
    "                    temp = [mat[i][j]]\n",
    "                    k = 0\n",
    "                k += 1\n",
    "        reshape_mat.append(temp)\n",
    "        return reshape_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        res = [[0 for _ in range(c)] for _ in range(r)]\n",
    "        for i in range(r*c):\n",
    "            res[i//c][i%c] = mat[i//n][i%n]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        itr = chain.from_iterable(mat)\n",
    "        return [[next(itr) for _ in range(c)] for _ in range(r)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n!=r*c:\n",
    "            return mat\n",
    "        else:\n",
    "            ans=[[0]*c for _ in range (r)]\n",
    "            for x in range(m*n):\n",
    "                ans[x//c][x%c]=mat[x//n][x%n]\n",
    "            return ans\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(nums), len(nums[0])\n",
    "        if m * n != r * c:\n",
    "            return nums\n",
    "        \n",
    "        ans = [[0] * c for _ in range(r)]\n",
    "        for x in range(m * n):\n",
    "            ans[x // c][x % c] = nums[x // n][x % n]\n",
    "        \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        rows = len(mat)\n",
    "        cols = len(mat[0])\n",
    "        count = rows * cols\n",
    "        if count != c * r:\n",
    "            return mat\n",
    "        ls = [j for i in mat for j in i]\n",
    "        res = []\n",
    "        for i in range(r):\n",
    "            res.append(ls[i*c : (i+1)*c])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        # xx\n",
    "        if len(mat) == 0 or len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        #\n",
    "        ret = [[]] * r\n",
    "        m, n = 0, 0\n",
    "        for i in range(r):\n",
    "            newRow = [0] * c\n",
    "            for j in range(c):\n",
    "                newRow[j] = mat[m][n]\n",
    "                n += 1\n",
    "                if n == len(mat[0]):\n",
    "                    m += 1\n",
    "                    n = 0\n",
    "            ret[i] = newRow\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n==r*c:\n",
    "            index=0\n",
    "            reshape=[[0]*c for i in range(r)]\n",
    "            for j in range(m):\n",
    "                for k in range(n):\n",
    "                    row=index//c \n",
    "                    col=index%c\n",
    "                    reshape[row][col]=mat[j][k]\n",
    "                    index+=1\n",
    "            return reshape\n",
    "        else:\n",
    "            return mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n!=r*c:return mat\n",
    "        l=[]\n",
    "        for i in mat:\n",
    "            l=l+i\n",
    "        l1=[]\n",
    "        for i in range(r):\n",
    "            l1.append(l[c*i:c*i+c])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        if m*n!=r*c:\n",
    "            return mat\n",
    "        new_mat = [[0]*c for k in range(r)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                t = n*i+j\n",
    "                new_i, new_j = t//c, t%c\n",
    "                new_mat[new_i][new_j] = mat[i][j]\n",
    "        return new_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if r*c != m*n:\n",
    "            return mat\n",
    "        \n",
    "        full_mat = [mat[i][j] for i in range(m) for j in range(n)]\n",
    "        empty_mat = []\n",
    "        temp = []\n",
    "        print(r,c)\n",
    "        for i in range(r):#r=4\n",
    "            interval = i*c\n",
    "            temp = []\n",
    "            for j in range(c):\n",
    "                temp.append(full_mat[j+interval])\n",
    "            empty_mat.append(temp)\n",
    "            \n",
    "\n",
    "        return empty_mat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        new_mat = [[0]*c for _ in range(r)]\n",
    "        for i in range(m*n):\n",
    "            new_mat[i//c][i%c] = mat[i//n][i%n]\n",
    "        return new_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        x = len(mat)\n",
    "        y = len(mat[0])\n",
    "        if (x * y != r * c):\n",
    "            return mat\n",
    "        mats = []\n",
    "        for i in range(x):\n",
    "            for j in range(y):\n",
    "                mats.append(mat[i][j])\n",
    "        #print(mats)\n",
    "        res = []\n",
    "        index = 0\n",
    "        for i in range(r):\n",
    "            row = []\n",
    "            for j in range(c):\n",
    "                row.append(mats[index])\n",
    "                index += 1\n",
    "            res.append(row)\n",
    "            #print(row)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if r*c != m*n:\n",
    "            return mat\n",
    "        \n",
    "        full_mat = [mat[i][j] for i in range(m) for j in range(n)]\n",
    "        empty_mat = []\n",
    "        temp = []\n",
    "        print(r,c)\n",
    "        for i in range(r):#r=4\n",
    "            interval = i*c\n",
    "            temp = []\n",
    "            for j in range(c):\n",
    "                temp.append(full_mat[j+interval])\n",
    "                print(temp)\n",
    "            empty_mat.append(temp)\n",
    "            \n",
    "\n",
    "        return empty_mat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        global n,v\n",
    "        n=0\n",
    "        e=len(mat)\n",
    "        v=len(mat[0]) \n",
    "        def get(L,t):\n",
    "            p=[]\n",
    "            global n,v\n",
    "            while t>0:\n",
    "                q=n//v\n",
    "                w=n%v\n",
    "                p.append(mat[q][w])\n",
    "                t-=1\n",
    "                n+=1\n",
    "            return p\n",
    "        s=0\n",
    "        out=[]\n",
    "        for x in mat:\n",
    "            s+=len(x)\n",
    "        if r*c!=s:\n",
    "            return mat \n",
    "        else:\n",
    "            for k in range(r):\n",
    "                out.append(get(mat,c))\n",
    "        return out\n",
    "\n",
    "                \n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat) \n",
    "        n = len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        flat = (mat[i][j] for i in range(m) for j in range(n))\n",
    "        reshaped_mat = [[next(flat) for _ in range(c)] for _ in range(r)]\n",
    "\n",
    "        return reshaped_mat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n!=r*c:\n",
    "            return mat\n",
    "        mat_new=[[0 for j in range(c)] for i in range(r)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                row=(i*n+j)//c\n",
    "                col=(i*n+j)%c\n",
    "                mat_new[row][col]=mat[i][j]\n",
    "        return mat_new\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        aa = []\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                aa.append(mat[i][j])\n",
    "        if r*c != len(aa) or len(aa) % c != 0:\n",
    "            return mat\n",
    "        else:\n",
    "            aaa = [[0 for _ in range(c)] for _ in range(r)]\n",
    "            k = 0\n",
    "            for i in range(len(aaa)):\n",
    "                for j in range(len(aaa[0])):\n",
    "                    aaa[i][j] = aa[k]\n",
    "                    k += 1\n",
    "            return aaa\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        col=len(mat[0])\n",
    "        row=len(mat)\n",
    "        if col*row!=r*c:\n",
    "            return mat\n",
    "        new=[[0]*c for i in range(r)]\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                pos_r=(c*i+j)//col\n",
    "                pos_c=c*i+j-col*pos_r\n",
    "                new[i][j]=mat[pos_r][pos_c]\n",
    "        return new\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if r*c==len(mat)*len(mat[0]):\n",
    "         out=[]\n",
    "         count=0\n",
    "         for i in range(r):\n",
    "             n=[]\n",
    "             for j in range(c):\n",
    "                 n+=[mat[count//len(mat[0])][count%len(mat[0])]]\n",
    "                 count+=1\n",
    "             out.append(n)\n",
    "         return out\n",
    "        else:\n",
    "            return mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if len(mat)*len(mat[0])!=r*c:\n",
    "            return mat\n",
    "        else:\n",
    "            tmat=[[0]*c for i in range(r)]\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    tmat[i][j]=mat[(i*c+j)//len(mat[0])][(i*c+j)%len(mat[0])]\n",
    "            return tmat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        lst = []\n",
    "        for i in mat:\n",
    "            lst += i\n",
    "        ret = []\n",
    "        if len(lst) != r*c:\n",
    "            return mat\n",
    "        else:\n",
    "            for i in range(r):\n",
    "                ret.append([])\n",
    "                for j in range(c):\n",
    "                    ret[i].append(lst[i*c+j])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n!=r*c:\n",
    "            return mat\n",
    "        else:\n",
    "            res=[[0]*c for i in range(r)]\n",
    "            mm=[]\n",
    "            for i in mat:\n",
    "                mm+=i\n",
    "            l=len(mm)\n",
    "            cal=0\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    res[i][j]=mm[cal]\n",
    "                    cal+=1\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        list1=[]\n",
    "        if len(mat)*len(mat[0])!=c*r:\n",
    "            return mat\n",
    "        ans=[[0]*c for i in range(r)]\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                list1.append(mat[i][j])\n",
    "        for a in range(r):\n",
    "            for b in range(c):\n",
    "                ans[a][b]=list1[a*c+b]\n",
    "        return ans\n",
    "\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if len(mat) * len(mat[0]) != r * c: return mat\n",
    "        mat_flattened = []\n",
    "        for mat_m in mat:\n",
    "            mat_flattened.extend(mat_m)\n",
    "        if r == 1: return [mat_flattened]\n",
    "        ret = []\n",
    "        for i in range(r):\n",
    "            start, end = i * c, (i + 1) * c\n",
    "            ret.append(mat_flattened[start:end])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        rowlen=len(mat)\n",
    "        collen=len(mat[0])\n",
    "        rlist=[]\n",
    "        \n",
    "        if rowlen*collen==r*c:\n",
    "            res=[[0 for i in range(c)] for j in range(r)]\n",
    "            #print(res)\n",
    "            for i in range(rowlen):\n",
    "                for j in range(collen):\n",
    "                    rlist.append(mat[i][j])\n",
    "            #print(rlist)\n",
    "            \n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    res[i][j]=rlist[0]\n",
    "                    rlist=rlist[1:]\n",
    "            return res\n",
    "        else:\n",
    "            return mat\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\r\n",
    "        result_list=[]\r\n",
    "        result_sub_list=[]\r\n",
    "        for sublist in mat:\r\n",
    "            for i in sublist:\r\n",
    "                result_sub_list.append(i)\r\n",
    "                if len(result_sub_list) == c:\r\n",
    "                    result_list.append(result_sub_list)\r\n",
    "                    result_sub_list=[]\r\n",
    "        if result_sub_list or len(result_list) != r:\r\n",
    "            return mat\r\n",
    "        return result_list            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        else:\n",
    "            nums = []\n",
    "            for i in mat:\n",
    "                for j in i:\n",
    "                    nums.append(j)\n",
    "        matans = []\n",
    "        for i in range(r):\n",
    "            roll = []\n",
    "            for j in range(c):\n",
    "                roll.append(nums[i*c+j])\n",
    "            matans.append(roll)\n",
    "        return matans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        length = len(mat)\n",
    "        width = len(mat[0])\n",
    "        lis = []\n",
    "        lis1 = []\n",
    "        if r*c != length*width:\n",
    "            return mat\n",
    "        for i in range(length):\n",
    "            for j in range(width):\n",
    "                lis.append(mat[i][j])\n",
    "        k = 0\n",
    "        for i in range(r):\n",
    "            lis1.append([])\n",
    "            for j in range(c):\n",
    "                lis1[i].append(lis[k])\n",
    "                k+=1\n",
    "        return lis1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        row = len(mat)  #原矩阵的行数n\n",
    "        col = len(mat[0]) #原矩阵的列数m\n",
    "        if row * col != r * c:\n",
    "            return mat\n",
    "        else:\n",
    "            anw = []\n",
    "            nums = []\n",
    "            list_nums = r\n",
    "            list_name = [\"list\" + str(i) for i in range(1,list_nums+1) ]\n",
    "            for i in range(len(list_name)):\n",
    "                list_name[i] = []\n",
    "                anw.append(list_name[i])\n",
    "            for i in mat:\n",
    "                for j in i:\n",
    "                    nums.append(j)\n",
    "            for x in range(r):\n",
    "                for y in range(c):\n",
    "                    anw[x].append(nums[y])\n",
    "                nums = nums[c:]\n",
    "            return anw\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        mat_row = len(mat)\n",
    "        mat_col = len(mat[0])\n",
    "        if mat_row * mat_col != r*c:\n",
    "            return mat\n",
    "        \n",
    "        res = [([0] * c) for p in range(r)]\n",
    "        print(res)\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                num = i * c + j\n",
    "                idx1 = num // mat_col\n",
    "                idx2 = num %  mat_col\n",
    "                res[i][j] = mat[idx1][idx2]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if r*c != m*n:\n",
    "            return mat\n",
    "        \n",
    "        full_mat = [mat[i][j] for i in range(m) for j in range(n)]\n",
    "        empty_mat = []\n",
    "        temp = []\n",
    "        print(r,c)\n",
    "        for i in range(r):#r=4\n",
    "            interval = i*c\n",
    "            temp = []\n",
    "            for j in range(c):\n",
    "                temp.append(full_mat[j+interval])\n",
    "                print(temp)\n",
    "            empty_mat.append(temp)\n",
    "            \n",
    "\n",
    "        return empty_mat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        # 求出总计\n",
    "        # 分行列\n",
    "   \n",
    "        count = len(mat) * len(mat[0])\n",
    "\n",
    "        tmp = [j for i in mat for j in i]\n",
    "        result = []\n",
    "        \n",
    "        if r * c != count:\n",
    "            return mat\n",
    "        else:\n",
    "            index = 0\n",
    "            for i in range(r):\n",
    "                init = []\n",
    "                for j in range(c):\n",
    "                    init.append(tmp[index])\n",
    "                    index += 1\n",
    "                result.append(init)\n",
    "\n",
    "                print(\"result:\", result)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        temp = sum(mat,[])\n",
    "        ans = [[0 for i in range(c)] for j in range(r)]\n",
    "        i = 0\n",
    "        for j in range(r):\n",
    "            for k in range(c):\n",
    "                ans[j][k] = temp[i]\n",
    "                i = i + 1\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        ans = [[0] * c for _ in range(r)]\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        for x in range(m*n):\n",
    "            ans[x//c][x%c]=mat[x//n][x%n]\n",
    "        return ans\n",
    "        \n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        tmp = []\n",
    "        t = []\n",
    "        for i in mat:\n",
    "            t += i\n",
    "        if r * c != len(t):\n",
    "            return mat\n",
    "        flag = 0\n",
    "        for i in t:\n",
    "            tmp.append(i)\n",
    "            flag += 1\n",
    "            if flag == c:\n",
    "                print(tmp)\n",
    "                res.append(tmp)\n",
    "                tmp = []\n",
    "                flag = 0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        if m * n != r * c:\n",
    "            return mat\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                res.append(mat[i][j])\n",
    "        print(res)\n",
    "        dp = [[0]*c for _ in range(r)]\n",
    "        print(\"dp\",dp)\n",
    "        k = 0\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                dp[i][j] = res[k]\n",
    "                k += 1\n",
    "        return dp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        if r*c != m*n:\n",
    "            return mat\n",
    "        \n",
    "        full_mat = [mat[i][j] for i in range(m) for j in range(n)]\n",
    "        empty_mat = []\n",
    "        temp = []\n",
    "        print(r,c)\n",
    "        for i in range(r):#r=4\n",
    "            interval = i*c\n",
    "            temp = []\n",
    "            for j in range(c):\n",
    "                temp.append(full_mat[j+interval])\n",
    "                print(temp)\n",
    "            empty_mat.append(temp)\n",
    "            print(empty_mat)\n",
    "            \n",
    "\n",
    "        return empty_mat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m,n=len(mat),len(mat[0])\n",
    "        nums=[]\n",
    "        for ma in mat:\n",
    "            for i in range(n):\n",
    "                nums.append(ma[i])\n",
    "        print(nums)\n",
    "        s=len(nums)\n",
    "        if s%r!=0:\n",
    "            return mat\n",
    "        # 每行列数\n",
    "        k=s//r\n",
    "        res=[]\n",
    "        i=0\n",
    "        while i<s:\n",
    "            res.append(nums[i:i+k])\n",
    "            i+=k\n",
    "        print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        if r * c != len(mat) * len(mat[0]):\n",
    "            return mat\n",
    "        res = []\n",
    "        tmp = []\n",
    "        t = []\n",
    "        for i in mat:\n",
    "            t += i\n",
    "        flag = 0\n",
    "        for i in t:\n",
    "            tmp.append(i)\n",
    "            flag += 1\n",
    "            if flag == c:\n",
    "                print(tmp)\n",
    "                res.append(tmp)\n",
    "                tmp = []\n",
    "                flag = 0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        output = [[0] * c for _ in range(r)]\n",
    "        print(output)\n",
    "        if m*n != r*c:\n",
    "            return mat\n",
    "        for x in range(m):\n",
    "            for y in range(n):\n",
    "                output[(x*n+y)//c][(x*n+y)%c] = mat[x][y]\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        ans=[[0]*c for _ in range(r)]\n",
    "        base=[]\n",
    "        n=len(mat)\n",
    "        m=len(mat[0])\n",
    "        if c*r!=n*m:\n",
    "            return mat\n",
    "        else:\n",
    "            for i in range(n):\n",
    "                for j in range(m):\n",
    "                    base.append(mat[i][j])\n",
    "            #print(len(base))\n",
    "            #return base[r*c-1]\n",
    "            #count=0\n",
    "            #print(base)\n",
    "            x=0\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    #print(i,j)\n",
    "                    #print(base[i*c+j])\n",
    "                    #print(base[i*c+j])  \n",
    "                    ans[i][j]=base[x]\n",
    "                    x += 1\n",
    "                    #print(ans[i][j])\n",
    "                    #count+=1\n",
    "                    #print(ans)\n",
    "                    #print(count)\n",
    "        return ans            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        n = 0\n",
    "        rt_mat = []\n",
    "        for ii in range(len(mat)):\n",
    "            for jj in range(len(mat[ii])):\n",
    "                ele = mat[ii][jj]\n",
    "                if n%c==0:\n",
    "                    rt_mat.append([])\n",
    "                rt_mat[-1].append(ele)\n",
    "                n+=1\n",
    "\n",
    "        return rt_mat if n==r*c else mat\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from ast import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat, r: int, c: int) :\n",
    "        '''\n",
    "        a, b\n",
    "        y = ax+b\n",
    "        :param mat:\n",
    "        :param r:\n",
    "        :param c:\n",
    "        :return:\n",
    "        '''\n",
    "\n",
    "        m, n = len(mat), len(mat[0])\n",
    "\n",
    "        res = [[0 for _ in range(c)] for __ in range(r)]\n",
    "        if m * n == r * c:\n",
    "\n",
    "            for x in range(m):\n",
    "                for y in range(n):\n",
    "                    total = x * n + y\n",
    "                    reminder = total // c\n",
    "                    extend = total % c\n",
    "                    '''\n",
    "                    0//5=0\n",
    "                    1//5=0\n",
    "                    2//5=0\n",
    "                    5//5=1\n",
    "                    6//5=1\n",
    "                    \n",
    "                    12//5=2\n",
    "                    15//5=3\n",
    "                    \n",
    "                    0%5=0\n",
    "                    1%5=1\n",
    "                    2%5=2\n",
    "                    5%5=0\n",
    "                    6%5=1\n",
    "                    '''\n",
    "                    res[reminder][extend] = mat[x][y]\n",
    "\n",
    "            return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        else:\n",
    "            return mat\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    mat = [[1,2],[3,4]]\n",
    "    r = 1\n",
    "    c = 4\n",
    "    print(Solution().matrixReshape(mat, r, c))\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        #arr = [[0] * c ] * r # 浅拷贝，不能这么搞\n",
    "        arr = [[0] * c for _ in range(r)]\n",
    "        if m * n != c * r:\n",
    "          return mat\n",
    "        for i in range(m * n):\n",
    "          arr[i // c][i % c] = mat[i // n][ i % n]\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        lst=[[0]*c for _ in range(r)]\n",
    "        if r*c!=m*n:\n",
    "            return mat\n",
    "        i,j=0,0\n",
    "        p,q=0,0\n",
    "        while i<m and j<n and p<r and q<c:\n",
    "            lst[p][q]=mat[i][j]\n",
    "            if j<n-1:\n",
    "                j+=1\n",
    "            else:\n",
    "                j=0\n",
    "                i+=1\n",
    "\n",
    "            if q<c-1:\n",
    "                q+=1\n",
    "            else:\n",
    "                q=0\n",
    "                p+=1\n",
    "        return lst\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        a,b,d,res=[],[mat[j][i] for j in range(len(mat)) for i in range(len(mat[0]))],0,[]\n",
    "        if r * c !=len(mat) * len(mat[0]):\n",
    "            return mat\n",
    "        \n",
    "        for k in range(r):\n",
    "            for l in range(c):\n",
    "                a.append(b[d])\n",
    "                d+=1\n",
    "\n",
    "            res.append(a)\n",
    "            print(res)\n",
    "\n",
    "            a=[]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m,n = len(mat), len(mat[0])\n",
    "        if m *n == r*c:\n",
    "            mat_new = [[0] * c for _ in range(r)]\n",
    "            print(mat_new)\n",
    "            for i in range(m):\n",
    "                for j in range(n):\n",
    "                    l = i*n+j\n",
    "                    print(i,j,l)\n",
    "                    # print(l)\n",
    "                    mat_new[l//c][l%c] = mat[i][j]\n",
    "            mat = mat_new\n",
    "        return mat\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        res=[[0]*c for i in range(r)]\n",
    "        if r*c!=len(mat)*len(mat[0]):\n",
    "            return mat\n",
    "\n",
    "        cnt=0\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                \n",
    "                res[cnt//c][cnt%c]=mat[i][j]\n",
    "                cnt+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        re_matrix = [[0] * c for _ in range(r)]\n",
    "        if len(mat) * len(mat[0]) != r * c:\n",
    "            return mat\n",
    "        r_count = 0\n",
    "        c_count = 0\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if c_count == c:\n",
    "                    c_count = 0\n",
    "                    r_count += 1\n",
    "                re_matrix[r_count][c_count] = mat[i][j]\n",
    "                c_count += 1\n",
    "        return re_matrix\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        a=len(mat)\n",
    "        b=len(mat[0])\n",
    "        matt=[[0]*c for i in range(r)]\n",
    "        if a*b!=r*c:\n",
    "            return mat\n",
    "        else:\n",
    "            rr=0\n",
    "            cc=0\n",
    "            for i in range(a):\n",
    "                for m in range(b):\n",
    "                    matt[rr][cc] = mat[i][m]\n",
    "                    cc +=1\n",
    "                    if cc == c:\n",
    "                        rr += 1\n",
    "                        cc =0\n",
    "            return matt\n",
    "                    \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        m,n = len(mat),len(mat[0])\n",
    "        ans = [[None]*c for i in range(r)]\n",
    "        if r*c != m * n:\n",
    "            return mat\n",
    "        else:\n",
    "            array = self.generator(mat)\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    ans[i][j] = next(array)\n",
    "            return ans\n",
    "    def generator(self,mat):\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                yield mat[i][j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "\n",
    "        p = [[0]*c for _ in range(r)]\n",
    "        m,n=len(mat),len(mat[0])\n",
    "        if m*n !=r*c :\n",
    "            return mat\n",
    "\n",
    "        for i in range(m*n):\n",
    "            p[i//c][i%c]=mat[i//n][i%n]\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n",
    "        ans=[[0]*c for _ in range(r)]\n",
    "        base=[]\n",
    "        n=len(mat)\n",
    "        m=len(mat[0])\n",
    "        if c*r!=n*m:\n",
    "            return mat\n",
    "        else:\n",
    "            for i in range(n):\n",
    "                for j in range(m):\n",
    "                    base.append(mat[i][j])\n",
    "            #print(len(base))\n",
    "            #return base[r*c-1]\n",
    "            #count=0\n",
    "            #print(base)\n",
    "            x=0\n",
    "            for i in range(r):\n",
    "                for j in range(c):\n",
    "                    #print(i,j)\n",
    "                    #print(base[i*c+j])\n",
    "                    #print(base[i*c+j])  \n",
    "                    ans[i][j]=base[x]\n",
    "                    x += 1\n",
    "                    #print(ans[i][j])\n",
    "                    #count+=1\n",
    "                    #print(ans)\n",
    "                    #print(count)\n",
    "        return ans            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
